#include "snake.h"

Snake::Snake(bool player2){
   this->dir = 1;
   
   this->snake_lengh = 3;
   for (int x = this->snake_lengh; x >= 0; x--) {
      this->snake_pos[x][0] = 12 + x;
      this->snake_pos[x][1] = 40;
   }
   
   this->second_player = player2;
}

void Snake::snakeMove(int z){
   
   if (!this->second_player){
      switch(z){
         case KEY_UP:
            if (this->dir == 4) break;
            this->dir = 1;
            break;
         
         case KEY_DOWN:
            if (this->dir == 1) break;
            this->dir = 4;
            break;
         
         case KEY_LEFT:
            if (this->dir == 3) break;
            this->dir = 2;
            break;
         
         case KEY_RIGHT:
            if (this->dir == 2) break;
            this->dir = 3;
            break;      

         default:
            break;
      }
   }
   else if (this->second_player){
      switch(z){
         case 'w':
            if (this->dir == 4) break;
            this->dir = 1;
            break;
         
         case 's':
            if (this->dir == 1) break;
            this->dir = 4;
            break;
         
         case 'a':
            if (this->dir == 3) break;
            this->dir = 2;
            break;
         
         case 'd':
            if (this->dir == 2) break;
            this->dir = 3;
            break;      

         default:
            break;
      }
   }
}

void Snake::snakeRun(){
   for(int x = this->snake_lengh - 1; x > 0; x--){      
      this->snake_pos[x][0] = this->snake_pos[x-1][0];
      this->snake_pos[x][1] = this->snake_pos[x-1][1];
      }   

   switch(this->dir){
      case 4:
         if(this->snake_pos[0][0] == 23) { this->snake_pos[0][0] = 0; break;}
         this->snake_pos[0][0]++;
         break;
    
      case 1:
         if(this->snake_pos[0][0] == 0) { this->snake_pos[0][0] = 23; break;}
         this->snake_pos[0][0]--;
         break;
   
      case 2:
         if(this->snake_pos[0][1] == 0) { this->snake_pos[0][1] = 79; break;}
         this->snake_pos[0][1]--;
         break;
   
      case 3:
         if(this->snake_pos[0][1] == 79) { this->snake_pos[0][1] = 0; break;}
         this->snake_pos[0][1]++;
         break;
   }
}

void Snake::snakeAppend(){
   this->snake_lengh += 1;
   
   if(this->snake_pos[this->snake_lengh-3][0] - this->snake_pos[this->snake_lengh-2][0] == 1) {
      this->snake_pos[this->snake_lengh - 1][0] = this->snake_pos[this->snake_lengh-2][0] - 1;
      }
   if(this->snake_pos[this->snake_lengh-3][0] - this->snake_pos[this->snake_lengh-2][0] == -1) {
      this->snake_pos[this->snake_lengh - 1][0] = this->snake_pos[this->snake_lengh-2][0] + 1;
      }
   if(this->snake_pos[this->snake_lengh-3][1] - this->snake_pos[this->snake_lengh-2][1] == -1) {
      this->snake_pos[this->snake_lengh - 1][1] = this->snake_pos[this->snake_lengh-2][1] + 1;
      }
   if(this->snake_pos[this->snake_lengh-3][1] - this->snake_pos[this->snake_lengh-2][1] == 1) {
      this->snake_pos[this->snake_lengh - 1][1] = this->snake_pos[this->snake_lengh-2][1] - 1;
      }
}

void Snake::snakeDie(){
   this->dir = 1;
   
   this->snake_lengh = 3;
   for (int x = this->snake_lengh; x >= 0; x--) {
      this->snake_pos[x][0] = 12 + x;
      this->snake_pos[x][1] = 40;
   }
}
